<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        style="width: 200px;"
        class="filter-item"
        placeholder="请输入姓名"
        v-model="queryForm.realName"
        maxlength="15"
      >
      </el-input>
      <el-input
        style="width: 200px;"
        class="filter-item"
        placeholder="请输入手机号"
        v-model="queryForm.phone"
        maxlength="11"
      >
      </el-input>
      <el-input
        style="width: 200px;"
        class="filter-item"
        placeholder="请输入身份证"
        v-model="queryForm.idcard"
        maxlength="18"
      >
      </el-input>
      <el-select
        clearable
        style="width: 150px"
        class="filter-item"
        v-model="queryForm.status"
        placeholder="请选择审核状态"
      >
        <el-option
          v-for="(val, key) in statusOptions"
          :key="key"
          :label="val"
          :value="key"
        >
        </el-option>
      </el-select>
      <el-button
        class="filter-item"
        @click="doQuery"
        type="primary"
        icon="el-icon-search"
        title="搜索"
        >搜索</el-button
      >
    </div>
    <el-table
      v-loading="loading"
      @sort-change="handleSortChange"
      :data="records"
      style="width: 100%"
    >
      <el-table-column prop="rowId" label="ID" width="100" align="center">
      </el-table-column>
      <el-table-column prop="realName" label="姓名" width="150" align="center">
      </el-table-column>
      <el-table-column prop="age" label="年龄" width="100" align="center">
      </el-table-column>
      <el-table-column prop="sex" label="性别" width="100" align="center">
        <template slot-scope="scope">
          {{ Dicts.USER_SEX[scope.row.sex] }}
        </template>
      </el-table-column>
      <el-table-column prop="phone" label="电话" width="150" align="center">
      </el-table-column>
      <el-table-column prop="idcard" label="身份证" width="250" align="center">
        <template slot-scope="scope">
          <template
            v-if="
              scope.row.idcard !== undefined && scope.row.idcard.length == 15
            "
          >
            {{
              scope.row.idcard.substr(0, 4) +
                '******' +
                scope.row.idcard.slice(11)
            }}
          </template>
          <template
            v-if="
              scope.row.idcard !== undefined && scope.row.idcard.length == 18
            "
          >
            {{
              scope.row.idcard.substr(0, 4) +
                '******' +
                scope.row.idcard.slice(14)
            }}
          </template>
        </template>
      </el-table-column>
      <el-table-column
        prop="companyName"
        label="公司名称"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="linkPhone"
        label="联系电话"
        width="150"
        align="center"
      >
        <template slot-scope="scope">
          <template v-if="scope.row.linkPhone !== undefined">
            {{
              scope.row.linkPhone.substr(0, 3) +
                '*****' +
                scope.row.linkPhone.slice(8)
            }}
          </template>
        </template>
      </el-table-column>
      <el-table-column
        prop="address"
        label="公司地址"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="businessLicense"
        label="营业执照"
        width="150"
        align="center"
      >
        <template slot-scope="scope" v-if="scope.row.businessLicense != null">
          <div v-viewer>
            <img
              :src="$oss(scope.row.businessLicense)"
              alt
              style="width:30px;height:30px;border-radius:5px;"
            />
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="150" align="center">
        <template slot-scope="scope">
          {{ Dicts.AUTHAPPLY_STATUS[scope.row.status] }}
        </template>
      </el-table-column>
      <el-table-column
        width="300"
        align="center"
        label="操作"
        class-name="small-padding"
        fixed="right"
      >
        <template slot-scope="scope">
          <template v-if="scope.row.status == 0">
            <el-button
              size="mini"
              type="success"
              @click="approveDialog(scope.row)"
              icon="el-icon-check"
              circle
              title="通过"
            ></el-button>
            <el-button
              size="mini"
              type="danger"
              @click="rejectDialog(scope.row.rowId)"
              icon="el-icon-close"
              circle
              title="驳回"
            ></el-button>
          </template>
          <el-button
            size="mini"
            type="info"
            @click="msgDialog(scope.row)"
            icon="el-icon-view"
            circle
            title="详情"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryForm.current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryForm.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="queryForm.total"
      >
      </el-pagination>
    </div>

    <el-dialog
      :title="`审批`"
      :visible.sync="dialogApproveFormVisible"
      width="40%"
    >
      <el-form :model="approveForm" label-width="90px" ref="approveForm">
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名:" prop="realName">
              {{ approveForm.realName }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话:" prop="linkPhone">
              {{ approveForm.linkPhone }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="身份证:" prop="idcard">
              {{ approveForm.idcard }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="公司名称:" prop="companyName">
              {{ approveForm.companyName }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司地址:" prop="address">
              {{ approveForm.address }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="营业执照:" prop="businessLicense">
              <img :src="$oss(approveForm.businessLicense)" width="60%" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="小区名称:" prop="communityNames">
              {{ approveForm.communityNames }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <img src="" />
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogApproveFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="approve(approveForm)"
          >确 定</el-button
        >
      </div>
    </el-dialog>

    <el-dialog :title="`详情`" :visible.sync="dialogMsgFormVisible" width="40%">
      <el-form :model="approveForm" label-width="90px" ref="approveForm">
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名:" prop="realName">
              {{ approveForm.realName }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话:" prop="linkPhone">
              {{ approveForm.linkPhone }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="身份证:" prop="idcard">
              {{ approveForm.idcard }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="公司名称:" prop="companyName">
              {{ approveForm.companyName }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司地址:" prop="address">
              {{ approveForm.address }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="营业执照:" prop="businessLicense">
              <img :src="$oss(approveForm.businessLicense)" width="60%" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="小区名称:" prop="communityNames">
              {{ approveForm.communityNames }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogMsgFormVisible = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  querypropertyCompanyApply,
  approvePropertyCompanyApply,
  rejectPropertyUserApply
} from '@/api/property'
import 'viewerjs/dist/viewer.css'
import Viewer from 'v-viewer'
import Vue from 'vue'
Vue.use(Viewer)
export default {
  name: 'PropertyCompanyApply',
  data() {
    return {
      dialogApproveFormVisible: false,
      dialogMsgFormVisible: false,
      loading: false,
      rejectFormRules: {
        remark: [{ required: true, message: '请输入驳回原因', trigger: 'blur' }]
      },
      queryForm: {
        current: 1,
        pages: 0,
        total: 0,
        size: 10,
        ascs: '',
        descs: '',
        importance: undefined,
        title: undefined
      },
      rejectForm: {
        rowId: '',
        remark: ''
      },
      approveForm: {},
      editExample: {},
      statusOptions: Dicts.AUTHAPPLY_STATUS,
      sexOptions: Dicts.USER_SEX,
      records: []
    }
  },
  created() {
    this.initOssClient().then(_ => {
      this.doQuery()
    })
  },
  methods: {
    handleSortChange({ column, prop, order }) {
      this.queryForm.ascs = ''
      this.queryForm.descs = ''
      if (order) {
        if (order === 'ascending') {
          this.queryForm.ascs = column.sortBy
        } else {
          this.queryForm.descs = column.sortBy
        }
      }
      this.queryForm.current = 0
      this.doQuery()
    },
    handleCurrentChange(current) {
      this.queryForm.current = current
      this.doQuery()
    },
    handleSizeChange(size) {
      this.queryForm.size = size
      this.doQuery()
    },
    doQuery() {
      this.loading = true
      querypropertyCompanyApply(this.queryForm).then(({ data: { data } }) => {
        this.queryForm.current = data.current
        this.queryForm.pages = data.pages
        this.queryForm.total = data.total
        this.queryForm.size = data.size
        this.records = data.records
        this.loading = false
      })
    },
    approveDialog({
      rowId,
      realName,
      linkPhone,
      idcard,
      companyCode,
      companyName,
      address,
      businessLicense,
      communityIds,
      communityNames
    }) {
      this.approveForm = {}
      this.dialogApproveFormVisible = true
      this.$refs.approveForm && this.$refs.approveForm.resetFields()
      this.approveForm.rowId = rowId
      this.approveForm.realName = realName
      this.approveForm.linkPhone = linkPhone
      this.approveForm.idcard = idcard
      this.approveForm.companyCode = companyCode
      this.approveForm.address = address
      this.approveForm.companyName = companyName
      this.approveForm.businessLicense = businessLicense
      this.approveForm.communityIds = communityIds
      this.approveForm.communityNames = communityNames
    },
    approve(approveForm) {
      this.editExample = approveForm
      this.editExample.rowId = approveForm.rowId
      this.editExample.communityIds = approveForm.communityIds
      this.editExample.status = '1'
      this.$confirm('确认通过此申请?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        approvePropertyCompanyApply(this.editExample).then(
          ({ data: { data, msg } }) => {
            this.dialogApproveFormVisible = false
            this.doQuery()
            this.$message({
              showClose: true,
              message: msg,
              type: 'success'
            })
          }
        )
      })
    },
    rejectDialog(rowId) {
      this.editExample.rowId = rowId
      this.editExample.status = '2'
      this.$confirm('确认驳回此申请?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        rejectPropertyUserApply(this.editExample).then(
          ({ data: { data, msg } }) => {
            this.dialogApproveFormVisible = false
            this.doQuery()
            this.$message({
              showClose: true,
              message: msg,
              type: 'success'
            })
          }
        )
      })
    },
    msgDialog({
      rowId,
      realName,
      linkPhone,
      idcard,
      companyCode,
      companyName,
      address,
      businessLicense,
      communityNames
    }) {
      this.approveForm = {}
      this.dialogMsgFormVisible = true
      this.$refs.approveForm && this.$refs.approveForm.resetFields()
      this.approveForm.rowId = rowId
      this.approveForm.realName = realName
      this.approveForm.linkPhone = linkPhone
      this.approveForm.idcard = idcard
      this.approveForm.companyCode = companyCode
      this.approveForm.address = address
      this.approveForm.companyName = companyName
      this.approveForm.businessLicense = businessLicense
      this.approveForm.communityNames = communityNames
    }
  },
  components: {}
}
</script>

<style scoped></style>
